package com.jblv.frame.common.utils;

import com.jblv.frame.common.constants.BaseConstant;
import com.jblv.frame.common.dto.LoginOperatorDTO;
import com.jblv.frame.common.exception.FrameException;
import com.jblv.frame.common.params.BaseParams;
import com.jblv.frame.common.utils.jwt.JwtUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author happyonion.yangtao
 * @version 1.0
 * @title SysUserAccessTokenUitls
 * @projectName jblv-frame
 * @packageName com.jblv.common.unils
 * @description SysUserAccessTokenUitls
 * @date Create at 2020/5/8 11:54
 * @since jdk1.8
 */
public class SysUserAccessTokenUitls {
    private static Logger logger = LoggerFactory.getLogger(SysUserAccessTokenUitls.class);

    public static String getOperatorAccessKtoken(String appId, String userId, String mobile, String nickname, String roleType, String userType) {
        Map<String, Object> params = new HashMap();
        params.put("appId", appId);
        params.put("userId", userId);
        params.put("mobile", mobile);
        params.put("nickname", nickname);
        params.put("roleType", roleType);
        params.put("userType", userType);
        params.put("loginTime", System.currentTimeMillis());
        String accessToken = JwtUtils.enhance(params);
        return accessToken;
    }


    public static String getOperatorAccessKtoken(String appId, String userId, String mobile, String username, String userType, String roleType, String avatar, String deptId, String deptName) {
        Map<String, Object> params = new HashMap();
        params.put("appId", appId);
        params.put("userId", userId);
        params.put("mobile", mobile);
        params.put("userName", username);
        params.put("userType", userType);
        params.put("roleType", roleType);
        params.put("avatar", avatar);
        params.put("deptId", deptId);
        params.put("deptName", deptName);
        params.put("loginTime", System.currentTimeMillis());
        String accessToken = JwtUtils.enhance(params);
        return accessToken;
    }

    public static String getUserType(HttpServletRequest request) throws FrameException {
        try {
            String token = request.getHeader(BaseConstant.TOKEN);
            if (StringUtils.isEmpty(token)) {
                return "";
            }

            Map<String, Object> params = JwtUtils.decode(token);
            if (params.containsKey("userType")) {
                return String.valueOf(params.get("userType"));
            }

            logger.error("no userType found in token header");
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return "";
    }

    public static BaseParams getBaseParams(HttpServletRequest request) throws FrameException {
        String baseParamsStr = request.getHeader(BaseConstant.PARAMS);
        BaseParams baseParams = null;

        try {
            baseParams =  GsonUtils.jsonToBean(baseParamsStr, BaseParams.class);
        } catch (Exception var4) {
        }

        return baseParams;
    }


    public static LoginOperatorDTO getLoginOperatorDTO(HttpServletRequest request) throws FrameException {
        String token = request.getHeader(BaseConstant.TOKEN);
        LoginOperatorDTO dto = getOperatorTokenDTO(token);
        return dto;
    }

    public static LoginOperatorDTO getOperatorTokenDTO(String token) {
        try {
            Map<String, Object> params = JwtUtils.decode(token);
            LoginOperatorDTO dto = new LoginOperatorDTO();
            Object obj = params.get("appId");
            dto.setAppId(String.valueOf(obj));
            obj = params.get("userId");
            dto.setUserId(String.valueOf(obj));
            obj = params.get("mobile");
            dto.setMobile(String.valueOf(obj));
            obj = params.get("userName");
            dto.setUserName(String.valueOf(obj));
            obj = params.get("roleType");
            if (obj != null) {
                dto.setRoleType((String)obj);
            }
            obj = params.get("userType");
            dto.setUserType(String.valueOf(obj));
            obj = params.get("avatar");
            dto.setAvatar(String.valueOf(obj));
            obj = params.get("deptId");
            if (obj != null) {
                dto.setDeptId(String.valueOf(obj));
            }
            obj = params.get("deptName");
            if (obj != null) {
                dto.setDeptName(String.valueOf(obj));
            }

            return dto;
        } catch (Exception var4) {
            var4.printStackTrace();
            return null;
        }
    }
}
